﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace tsgl
{
    public partial class userManage : Form
    {
        public userManage()
        {
            InitializeComponent();
        }

        private void userManage_Load(object sender, EventArgs e)
        {
            initialize();
            controEnableSet(true);
            btnEnabledSet(true);
            select();
        }

        private void select()
        {
            DataTable dt = bookDbClass.ExecuteDataTable("select * from 用户信息");
            this.dataGridView1.DataSource = dt.DefaultView;

            this.txtId.DataBindings.Clear();
            this.txtUserName.DataBindings.Clear();
            this.txtPassword.DataBindings.Clear();
            this.cboPermission.DataBindings.Clear();
            this.dtpStartDate.DataBindings.Clear();
            this.rbStop.DataBindings.Clear();
            this.rbUse.DataBindings.Clear();
            this.txtId.DataBindings.Add("text",dt.DefaultView,"用户编号");
            this.txtUserName.DataBindings.Add("text",dt.DefaultView,"用户名");        
            this.txtPassword.DataBindings.Add("text",dt.DefaultView,"密码");   
            this.cboPermission.DataBindings.Add("text",dt.DefaultView,"用户类型");
            this.dtpStartDate.DataBindings.Add("text",dt.DefaultView,"启用日期");
            this.rbUse.DataBindings.Add("checked", dt.DefaultView, "是否停用");
            //this.rbStop.DataBindings.Add("checked", dt.DefaultView, "是否停用");
            //if (dt.Rows[this.dataGridView1.CurrentCell.RowIndex]["是否停用"].ToString() == "1")
            //{
            //    this.rbUse.Checked = true;
            //}
            //else
            //{
            //    this.rbUse.Checked = false;
            //}

            //if (dt.Rows[this.dataGridView1.CurrentCell.RowIndex]["是否停用"].ToString() == "0")
            //{
            //    this.rbStop.Checked = true;
            //}
            //else
            //{
            //    this.rbStop.Checked = false;
            //}


            DataTable dt1 = bookDbClass.ExecuteDataTable("select 用户类型名称 from 用户类型");
            this.cboPermission.DataSource = dt1.DefaultView;
            this.cboPermission.DisplayMember = "用户类型名称";
        }


        private void initialize()
        {
            this.txtUserName.Text = "";
            this.txtPassword.Text = "";
            this.cboPermission.SelectedText = "";
            this.cboPermission.Text = "";
            this.cboPermission.SelectedIndex = -1;
            this.dtpStartDate.Text = DateTime.Now.ToString();
        }

        private void controEnableSet(Boolean state)
        {
            this.txtUserName.ReadOnly = state;
            this.txtPassword.ReadOnly = state;
            this.cboPermission.Enabled = !state;
            this.dtpStartDate.Enabled = !state;
            this.rbUse.Enabled = !state;
            this.rbStop.Enabled = !state;
        }

        private void btnEnabledSet(Boolean state)
        {
            this.btnAdd.Enabled = state;
            this.btnEidt.Enabled = state;
            this.btnDelete.Enabled = state;
            this.btnSave.Enabled = !state;
            this.btnCancel.Enabled = !state;
        }

  

        private string strFlag;
        private void btnAdd_Click(object sender, EventArgs e)
        {
            strFlag = "Add";
            initialize();
            controEnableSet(false);
            btnEnabledSet(false);
            this.txtUserName.Focus();
        }

        private void btnEidt_Click(object sender, EventArgs e)
        {
            strFlag = "Edit";
            controEnableSet(false);
            btnEnabledSet(false);
            txtUserName.Focus();
        }


        private static Boolean userDelete(string id)
        {
            if (bookDbClass.deleteByKey("用户信息", "用户编号", id) == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确实要删除该用户吗？", "删除提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                if (bookDbClass.deleteByDt(this.dataGridView1.CurrentCell.RowIndex, "select * from 用户信息") == true)
                {
                    select();
                    MessageBox.Show("成功删除一条记录！", "提示信息");
                }

                //userDelete(this.txtId.Text.ToString());
                //select();
                //MessageBox.Show("删除成功！");

                //bookDbClass.ExecuteNonQurey("delete from 用户信息 where 用户编号=@id", new SqlParameter("id", id));
                //string sql = "delete from 用户信息 where 用户编号={0}";
                //sql = string.Format(sql, id);
                //SqlCommand cmd = new SqlCommand(sql, conn);
                ////cmd.Parameters.Add("@id", SqlDbType.Int, 10);
                ////cmd.Parameters["@id"].Value = id;
                //int i = cmd.ExecuteNonQuery();
                //if (i == 0)
                //{
                //    MessageBox.Show("删除失败！");
                //}
                //else
                //{
                //    select();
                //    MessageBox.Show("删除成功！");
                //}

                //    select();
                //    MessageBox.Show("删除成功！");
                //}
            }
            else
            {
                MessageBox.Show("请指定删除对象！","错误提示信息");
            }
        }

        
        private void btnSave_Click(object sender, EventArgs e)
        {
            string str1 = this.txtUserName.Text;
            string str2 = this.txtPassword.Text;
            string str3 = this.cboPermission.Text;
            DateTime str4 = DateTime.Parse(this.dtpStartDate.Text);
            string str5;
            if (rbUse.Checked)
            {
                str5 = "1";
            }
            else
            {
                str5 = "0";
            }

            if (strFlag == "Add")
            {
                if (txtUserName.Text == "" | txtPassword.Text == "" | this.cboPermission.Text == "")
                {
                    MessageBox.Show("用户名、密码或用户类型不能为空！");
                }
                else
                {
                    bookDbClass.ExecuteNonQurey("insert into 用户信息(用户名,密码,用户类型,启用日期,是否停用) values(@name,@pw,@p,@sd,@ty)", new SqlParameter("name", str1), new SqlParameter("pw", str2), new SqlParameter("p", str3), new SqlParameter("sd", str4), new SqlParameter("ty", str5));
                    select();
                    btnEnabledSet(true);
                    MessageBox.Show("已成功新增一条记录！");
          　    }
            }

            if (strFlag == "Edit")
            {
                this.txtId.Visible = true;
                int i = Convert.ToInt32(this.txtId.Text.ToString());
                this.txtId.Visible = false;
                bookDbClass.ExecuteNonQurey("update 用户信息 set 用户名=@name,密码=@pw,用户类型=@p,启用日期=@sd,是否停用=@ty where 用户编号=@id", new SqlParameter("name", str1), new SqlParameter("pw", str2), new SqlParameter("p", str3), new SqlParameter("sd",str4), new SqlParameter("ty",str5),new SqlParameter("id",i));
                select();
                btnEnabledSet(true);
                MessageBox.Show("该记录修改成功！");
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            initialize();
            controEnableSet(true);
            btnEnabledSet(true);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
        {

        }
    }
}